package Offer.offer2016;

public class Test20
{

	public static void main(String[] args) 
	{
		int[][] matrix={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
		int[][] matrix1 = {{1,2,3,4}};
		int[][] matrix2 = {{1},{2},{3},{4}};
//		printMatrix(matrix1);
		CirclePrintArr.printMatrix(matrix2);
	}
    public static void printMatrix(int[][]matrix)
    {
    	int rows=matrix.length;
    	int columns=matrix[0].length;
    	if(matrix==null||rows==0||columns==0)
    		return;
    	int start=0;
    	while(rows>2*start&&columns>2*start)
    	{
    		printCircle(matrix,rows,columns,start);
    		start++;
    	}
    }
    public static void printCircle(int[][] matrix,int rows,int columns,int start)
    {
    	int endX = rows-start-1;
    	int endY = columns-start-1;
    	for(int i=start;i<=endY;++i)
    	{
    		System.out.print(matrix[start][i]);
    		System.out.print(" ");
    	}
    	if(endX>start)
    	{
    		for(int i=start+1;i<=endX;++i)
    		{
    			System.out.print(matrix[i][endY]);
    			System.out.print(" ");
    		}
    	}
    	if(endX>start&&endY>start)
    	{
    		for(int i=endY-1;i>=start;i--)
    		{
    			System.out.print(matrix[endX][i]);
    			System.out.print(" ");
    		}
    	}
    	if(endX>start+1&&endY>start)
    	{
    		for(int i=endX-1;i>start;i--)
    		{
    			System.out.print(matrix[i][start]);
    			System.out.print(" ");
    		}
    	}
    }
}
class CirclePrintArr{
	public static void printMatrix(int [][]a){
		if (a == null || a.length == 0) {
			return;
		}
		int rows = a.length;
		int cols = a[0].length;
		int start = 0;
		while(rows > 2 * start && cols > 2 * start){
			circlePrint(a,start);
			start++;
		}
	}
	public static void circlePrint(int [][]a,int start){
		int endx = a.length - start - 1;
		int endy = a[0].length - start - 1;
		for(int j = start; j <= endy; j++){
			System.out.print(a[start][j] + " ");
		}
		for (int i = start + 1; i <= endx; i++) {
			System.out.print(a[i][endy] + " ");
		}
		if (start < endx) {
			for (int j = endy - 1; j >= start; j--) {
				System.out.print(a[endx][j] + " ");
			}
		}
		if(start < endy){
			for (int i = endx - 1; i > start; i--) {
				System.out.print(a[i][start] + " ");
			}
		}
	}
}